

#include <stdlib.h>
#include <stdio.h>
#include <GLES/gl.h> /* For NDK*/
#include <GLES/glext.h>
#include <android/log.h>
#include "utils.h"
#include "jade.h"




jade_font_t * 
jade_font_create (int charWidth, int charHeight,
        int fontWidth, int fontHeight,
        const char *texName) {

    jade_font_t *font = NULL;
    jade_texture_t *texture = NULL;

    font = (jade_font_t*) malloc (sizeof(jade_font_t));
    if (font == NULL)
        return NULL;

    font->m_charWidth = charWidth;
    font->m_charHeight = charHeight;
    font->m_fontWidth = fontWidth;
    font->m_fontHeight = fontHeight;

    texture = (jade_texture_t*)jade_texture_create (texName); 
    if (texture == NULL) {
        FREEANDNULL (font);
        return NULL;
    }

    font->m_colCount = texture->width / font->m_charWidth;
    font->m_rowCount = texture->height / font->m_charHeight - 1;
    font->m_textureId = texture->textureId;

    jade_texture_destroy (texture);

    return font;

}

void jade_font_destroy (jade_font_t *font) {

   FREEANDNULL (font); 
}


int jade_font_get_width (jade_font_t *font) {

    return font->m_fontWidth;
}

int jade_font_get_height (jade_font_t *font) {

    return font->m_fontHeight;
}

void 
jade_font_draw_string (jade_font_t *font, int x, int y, const char *str) {

    //LOGD ("I am jade_font_draw_string.\n");

#ifndef GL_OES_draw_texture
#ifdef DEBUG
    LOGD ("Unsupport GL_OES_draw_texture extension...\n");
#endif
#else

    int i = 0, index = -1;
    int x_idx = 0, y_idx = 0;


    glPushMatrix();

    glShadeModel(GL_FLAT);//GL_SMOOTH,GL_FLAT
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_FOG);
    glEnable(GL_BLEND);
    glEnable(GL_DITHER);
    glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE); //GL_SRC_ALPHA

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, font->m_textureId);

    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glColor4f(1.0f, 0.0f, 0.0f, 0.0f);


    for (i = 0; i < strlen(str); i++) {

            index = (int)(str[i] - 32);

#if (defined(DEBUG))
            LOGD ("str[%d]: %c, index: %d\n", i, str[i], index);
#endif
            GLint crop[4] = { 0, 0, font->m_charWidth, font->m_charHeight };

            x_idx = index % font->m_colCount;
            y_idx = (int)(index / font->m_colCount);
            crop[0] = x_idx * font->m_charWidth;
            crop[1] = (font->m_rowCount - y_idx) * font->m_charHeight;

#if (defined(DEBUG))
            LOGD ("x_idx: %d, y_idx: %d, crop[0]: %d, crop[1]: %d\n", x_idx, y_idx, crop[0], crop[1]);
#endif

            glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
            glDrawTexiOES(x + i * font->m_fontWidth, y, 0, font->m_fontWidth, font->m_fontHeight);
    }

    glPopMatrix();

    glDisable(GL_TEXTURE_2D);
    glShadeModel(GL_SMOOTH);//GL_SMOOTH,GL_FLAT
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);
    glDisable(GL_DITHER);


#endif


}







